package com.bookmanager.www.controller;

import com.bookmanager.www.common.utils.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/10/02/1:37
 * @Description:
 */
@RestController
@Api(tags="验证码管理")
@RequestMapping("/code")
public class CodeController {

    @Resource
    RedisUtils redisUtils;
    @Resource
    RedisTemplate redisTemplate;

    /**
     * 生产验证码 保存到redis数据库中，有效时间为五分钟
     */
    @GetMapping("/createModifyCode")
    @ApiOperation(value="生成修改/更新验证码",notes="生产验证码 保存到redis数据库中，有效时间为五分钟")
    public Result createModifyCode(HttpServletRequest req) {
        try {
            String ip = req.getRemoteAddr();
            //获取6位数验证码
            String yzm = String.valueOf(CommonsUtils.getCode());
            redisUtils.set(ip+"_ModifyYzm", yzm, (long) (60*1000*3),TimeUnit.MILLISECONDS);
            System.out.println(redisUtils.get(ip+"_ModifyYzm"));
            return Result.succ("成功修改/更新验证码成功！","先不给你看~");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return Result.fail();
    }


    /**
     * 生产验证码 保存到redis数据库中，有效时间为五分钟
     */
    @GetMapping("/createCode")
    @ApiOperation(value="生成验证码",notes="生产验证码 保存到redis数据库中，有效时间为五分钟")
    public Result doYzm(HttpServletRequest req) {
        try {
            String ip = req.getRemoteAddr();
            //获取6位数验证码
            String yzm = String.valueOf(CommonsUtils.getCode());
           redisUtils.set(ip+"_yzm", yzm, (long) (60*1000*3),TimeUnit.MILLISECONDS);
            System.out.println(redisUtils.get(ip +"_yzm"));
            return Result.succ("成功验证码成功！","先不给你看~");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return Result.fail();
    }


    /**
     * 获取验证码并通过邮箱发送出去
     */
    @GetMapping("/sendEmail")
    @ApiOperation(value="邮箱发送验证码"  ,notes = "1 注册验证码 2 进去之后修改邮箱验证码 3 解绑邮箱")
    Result sendEmail(HttpServletRequest req,@RequestParam("email")String email,@RequestParam("type")String codeType)
    {

        String ip = req.getRemoteAddr();

        String code = (String) redisUtils.get(ip+"_yzm");
        if(("2").equals(codeType))
        {
            System.out.println(codeType+"是为2");
            code=(String)redisUtils.get(ip+"_ModifyYzm");
        }
        boolean sendEmail = EmailUtils.sendEmail(email, code,codeType);
        if(sendEmail)
        {
            return Result.succ("发送邮件成功！！","");
        }
        return Result.fail("发送邮件失败","可能是网络原因");
    }


    /**
     * 获取验证码并通过手机号发送
     */
    @GetMapping("/sendPhoneCode")
    @ApiOperation(value="手机号发送验证码注册或者登录用户或者新增手机号",notes="Type: 1 : 注册验证码  2:登录验证码 " +
            "3：进去之后修改/新增手机号验证码" +"4: 解绑手机号   5: 修改支付密码" )
    Result sendPhoneCodeForRegister(HttpServletRequest req,@RequestParam("phoneNum")String phoneNum,@RequestParam("type")String type)
    {
        String ip = req.getRemoteAddr();
        String code = (String) redisUtils.get(ip + "_yzm");
        if(type.equals("3"))
        {
            System.out.println("当前ip是:"+ip);
            System.out.println("Redis库中的验证码为："+redisUtils.get(ip+"_ModifyYzm"));
            code=(String)redisUtils.get(ip+"_ModifyYzm");
        }
        System.out.println("传入的手机号是："+phoneNum+"---生成的验证码是："+code);
        int codeType = Integer.parseInt(type);
        boolean sendCode = PhoneCodeUtils.sendCode(phoneNum, code,codeType);
        if(sendCode)
        {
            return Result.succ("发送验证码成功 注意查收","");
        }
        return Result.fail("验证码发送失败！","");
    }



    /**
     *     校验验证码
     */
    @ApiOperation(value="校验验证码")
    @PostMapping("/verifyCode")
    public Result login(HttpServletRequest req,@RequestParam("yzm")String yzm) {
        String ip = req.getRemoteAddr();
        String old_yzm = (String) redisUtils.get(ip + "_yzm");
        if (old_yzm == null) {
            return Result.fail("验证码不存在或者过期","");
        } else if (old_yzm.equals(yzm))
        {
            // 验证成功 自己删除那个键就好了。
            System.out.println("该验证码是否删除成功？？"+redisTemplate.delete(ip + "_yzm"));
            return Result.succ("验证码正确","");
        }
        else
        {
            return Result.fail("验证码错误","");
        }
    }
    /**
     *   校验修改更新验证码
     */
    @ApiOperation(value="校验修改/更新验证码")
    @PostMapping("/verifyModifyCode")
    public Result verifyModifyCode(HttpServletRequest req,@RequestParam("yzm")String yzm) {
        String ip = req.getRemoteAddr();
        System.out.println("ip======>"+ip);
        String old_yzm = (String) redisUtils.get(ip + "_ModifyYzm");
        if (old_yzm == null) {
            return Result.fail("验证码不存在或者过期","");
        } else if (old_yzm.equals(yzm))
        {
            // 验证成功 自己删除那个键就好了。
            System.out.println("该验证码是否删除成功？？"+redisTemplate.delete(ip + "_ModifyYzm"));
            return Result.succ("验证码正确","");
        }
        else
        {
            return Result.fail("验证码错误","");
        }
    }

}

